package com.qf.application.controller;

import com.qf.application.commons.utils.R;
import com.qf.application.entity.Emp;
import com.qf.application.service.IEmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/emp")
@Slf4j
public class EmpController {

    @Autowired
    private IEmpService empService;

    // 1、接口上面不能写多个对象，而且用RequestBody
    @PostMapping("/page")
    public R page(@RequestBody Map<String,Object> param) {
        return R.ok(empService.empPage(param));
    }

    // SpringMVC默认使用jackson来做JSON字符串的转换的
    @GetMapping("/list")
    public R list() {
        return R.ok(empService.list());
    }

    @PostMapping("/save")
    public R save(@RequestBody Emp emp) {
        int x= 10/emp.getMgr();
        emp.setCreateTime(new Date());
        return R.write(empService.save(emp));
    }

    @PostMapping("/update")
    public R update(@RequestBody Emp emp) {
        return R.write(empService.updateById(emp));
    }

    @GetMapping("/info/{empno}")
    public R info(@PathVariable Integer empno) {
        Emp byId = empService.getById(empno);
        return R.ok(byId);
    }

    @PostMapping("/remove")
    public R remove(@RequestBody List<Integer> empnoList) {
        boolean flag = empService.removeBatchByIds(empnoList);
        return R.write(flag);
    }

    @GetMapping("/testCookie")
    public void test(HttpServletResponse response) {
        Cookie cookie = new Cookie("k1", "v1");
        cookie.setPath("/"); // 当前域名
        cookie.setMaxAge(60 * 60 * 24);
        response.addCookie(cookie);
    }


}
